//加载列表
$(function(){
	$.ajax({
		type:"POST",
		url:"http://localhost:3000/server/findAll",
		dataType:"json",
		beforeSend: function (XMLHttpRequest) {
			XMLHttpRequest.setRequestHeader("Authorization", localStorage.getItem("doubanToken"))
		},
		success:function(x,y,z){
			var movie=$("#movie");
			var m=0;
			for(var n in x){
				if((n-0)%4==0){
					movie.append("<div class='container top-20'></div>");
					m++;
				}
				movie.children(".container:nth-of-type("+m+")").append("<div class='col-md-3'>"+
			"<div class='img'><a href='/movie/"+n+"?"+x[n]._id+"' ><img src="+x[n].poster+"></a></div>"+
			"<div>片名:"+x[n].title+"</div>"+
			"<div>导演:"+x[n].doctor+"</div>"+
			"<div>国家:"+x[n].country+"</div>"+
			"<div>语言:"+x[n].language+"</div>"+
			"<div>年代:"+x[n].year+"</div>"+
			"</div>")
			}
		},
		error:function(jqXHR){
		console.log(jqXHR);
		}
	})
});

//登录框弹出与关闭
function logShow(){
	$("#removeaButton").click();
	$("#logOnDiv").show(500);
}

$(function(){
	$("#logOn").click(function(){
		logShow();
	});
	$("#removeButton").click(function(){
		$("#logOnDiv").hide(500);
		$("#logInformation").html("");
		$("#logName").val("");
		$("#logPass").val("");
	});
});

//登录
$(function(){
	$("#logOnGo").click(function(){
		var logName=$("#logName").val();
		var logPass=$("#logPass").val();
		var logInformation=$("#logInformation");
		if(logName==""){
			logInformation.html("请输入用户名");
		}else if(logPass==""){
			logInformation.html("请输入密码");
		}else{
			$.ajax({
				type:"POST",
				url:"http://localhost:3000/server/haveUser",
				dataType:"json",
				data:{
					name:logName,
					password:logPass
				},
				success:function(x,y,z){
					if(x.error){
						logInformation.html(x.error);
					}else{
						console.log(x)
						localStorage.setItem('doubanToken', x[0].token)
						logInformation.html("登录成功");
						$("#removeButton").click();
						var navigation=$("#navigation div");
						navigation.empty();
						navigation.append(
							"<img class='head' src="+x[0].head+">"+
							"<button class='btn btn-info'>"+x[0].name+"</button>"+
							"<button class='btn btn-info' onclick='signOut()'>"+"退出"+"</button>"
						);
						if(x[0].identity=="admin"){
							navigation.append("<button class='btn btn-info' onclick='amAdmin()'>"+"控制台"+"</button>")
						};
						if(localStorage.getItem("name")!=x[0].name){
							writeLocal(x[0].head,x[0].name,x[0].identity);
						}else if(localStorage.getItem("name")==x[0].name){
							localStorage.setItem("signOut",0);
						}
					}
				},
				error:function(jqXHR){
					console.log(jqXHR);
				}
			});
		}
	});
});

//退出登录状态
function signOut(){
	var navigation=$("#navigation div");
	navigation.empty();
	navigation.append(
		"<button class='btn btn-info' onclick='logShow()'>"+"登 录"+"</button>"+
		"<button class='btn btn-info' id='register' onclick='registerShow()'>"+"注 册"+"</button>"
	);
	localStorage.setItem("signOut",1);
}

//注册框弹出与关闭
$(function(){
	$("#register").click(function(){
		registerShow();
	});
	$("#removeaButton").click(function(){
		$("#registerDiv").hide(500);
		$("#regInformation").html("");
		$("#regName").val("");
		$("#regPass").val("");
	});
});

function registerShow(){
	$("#removeButton").click();
	$("#registerDiv").show(500);
}

//注册
$(function(){
	$("#registerGo").click(function(){
		var regName=$("#regName").val();
		var regPass=$("#regPass").val();
		var regInformation=$("#regInformation");
		var space=/[\s]/;
		if(regName==""){
			regInformation.html("请输入用户名");
		}else if(regPass==""){
			regInformation.html("请输入密码");
		}else if(space.test(regName)){
			regInformation.html("用户名不能包含空格");
		}else if(space.test(regPass)){
			regInformation.html("密码不能包含空格");
		}else{
			$.ajax({
				type:"POST",
				url:"http://localhost:3000/server/register",
				dataType:"json",
				data:{
					name:regName,
					password:regPass
				},
			  	success:function(x,y,z){
			  		if(x.error){
			  			regInformation.html(x.error);
			  		}else{
			  			regInformation.html("注册成功");
			  			$("#removeaButton").click();
			  			var navigation=$("#navigation div");
			  			navigation.empty();
			  			navigation.append(
			  				"<img class='head' src="+x[0].head+">"+
			  				"<button class='btn btn-info'>"+x[0].name+"</button>"+
			  				"<button class='btn btn-info' onclick='signOut()'>"+"退出"+"</button>"
			  			)
			  		};
			  		writeLocal(x[0].head,x[0].name,x[0].identity);
			  	},
			  	error:function(jqXHR){
					console.log(jqXHR);
			  	}
			});
		}
	});
});

//获取本地储存
$(function(){
	var signOut=localStorage.getItem("signOut");
	if(signOut&&!(signOut-0)){
		var head=localStorage.getItem("head");
		var name=localStorage.getItem("name");
		var identity=localStorage.getItem("identity");
		var navigation=$("#navigation div");
		navigation.empty();
		navigation.append(
			"<img class='head' src="+head+">"+
			"<button class='btn btn-info'>"+name+"</button>"+
			"<button class='btn btn-info' onclick='signOut()'>"+"退出"+"</button>"
		);
		if(identity=="admin"){
			navigation.append("<button class='btn btn-info' onclick='amAdmin()'>"+"控制台"+"</button>")
		};
	}
});

//写入本地存储
function writeLocal(headSrc,name,identity){
	localStorage.setItem("head",headSrc);
	localStorage.setItem("name",name);
	localStorage.setItem("identity",identity);
	localStorage.setItem("signOut",0);
}

//管理员
function amAdmin(){
	window.location.href="http://localhost:3000/admin";
}